<template>
    <div>
        <div class="formContainer">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm jform">
                <el-form-item label="图书编号" prop="bkId" label-width="120px">
                    <el-input v-model="ruleForm.bkId" style="width: 217px;"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即查询</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>


        </div>
        <el-divider></el-divider>
        <div class="tableContainer">
            <el-table :data="queryResult" stripe size="medium">
                <el-table-column prop="bkId" label="编号"></el-table-column>
                <el-table-column prop="bkName" label="书名"></el-table-column>
                <el-table-column prop="bkAuthor" label="作者"></el-table-column>
                <el-table-column prop="bkPress" label="出版社"></el-table-column>
                <el-table-column prop="bkDatePress" label="出版时间"></el-table-column>
                <el-table-column prop="bkISBN" label="ISBN"></el-table-column>
                <el-table-column prop="bkCatalog" label="分类"></el-table-column>
                <el-table-column prop="bkLanguage" label="语言"></el-table-column>
                <el-table-column prop="bkPages" label="页数"></el-table-column>
                <el-table-column prop="bkPrice" label="价格"></el-table-column>
                <el-table-column prop="bkDateIn" label="入馆时间"></el-table-column>
                <el-table-column prop="bkStatus" label="状态"></el-table-column>
                <el-table-column prop="bkBrief" label="书籍简介"></el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="100">
                    <template slot-scope="scope">
                        <el-button @click="handleClickDelete()" type="text" size="small">删除</el-button>
                        <el-button @click="handleClickBorrow(scope.row)" type="text" size="small">借书</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-divider></el-divider>

        <el-drawer
                title="借书详情"
                :visible.sync="drawer"
                :direction="direction"
                :before-close="handleClose">

            <div class="formContainer">
                <el-form :model="borrowForm" ref="borrowForm" :rules="borrowRules" label-width="100px"
                         class="demo-ruleForm jform">
                    <el-form-item label="读者编号" prop="rdId" label-width="120px">
                        <el-input v-model="borrowForm.rdId" style="width: 217px;" disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="读者姓名" prop="rdName" label-width="120px">
                        <el-input v-model="borrowForm.rdName" style="width: 217px;" disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="图书编号" prop="bkId" label-width="120px">
                        <el-input v-model="borrowForm.bkId" style="width: 217px;" disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="书名" prop="bkName" label-width="120px">
                        <el-input v-model="borrowForm.bkName" style="width: 217px;" disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="预期归还时间" prop="ldDateRetPlan" label-width="120px">
                        <el-col :span="11">
                            <el-date-picker type="date" placeholder="选择日期" v-model="borrowForm.ldDateRetPlan"
                                            style="width: 100%;" value-format="yyyy-MM-dd hh:MM:ss"></el-date-picker>
                        </el-col>
                    </el-form-item>

                    <el-form-item label="借书操作员" prop="operatorBorrow" label-width="120px">
                        <el-input v-model="borrowForm.operatorBorrow" style="width: 217px;"  disabled="true"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="commitBorrow">借书</el-button>
                    </el-form-item>
                </el-form>


            </div>

        </el-drawer>
    </div>
</template>

<script>
    import {deleteBook, borrowBook} from "@/api";
    import {mapState} from 'vuex'

    export default {
        name: "",
        data() {
            return {
                drawer: false,
                direction: 'rtl',
                searchParams: {},
                ruleForm: {
                    bkId:null,
                    id: null,
                    name: '',
                    BkNO: '',
                    author: '',
                    press: '',
                    date: '',
                    language: '',
                    pageNum: '',
                    price: '',
                    ISBN: '',
                    category: '',
                    content: '',
                    qty: '',
                    enterDate: '',
                    status: ''
                },
                borrowForm: {
                    rdId: 0,
                    rdName: '',
                    bkId: 0,
                    bkName: '',
                    ldContinuesTimes: 0,
                    ldDateOut: '',
                    ldDateRetPlan: '',
                    ldDateRetAct: '',
                    ldOverDay: 0,
                    ldOverMoney: 0,
                    ldPunishMoney: 0,
                    isHasReturn: '',
                    operatorBorrow: '',
                    operatorReturn: ''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入书名', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 1 到 20个字符', trigger: 'blur'}
                    ]
                },
                borrowRules: {
                    ldDateRetPlan: [
                        {required: true, message: '请选择预计归还时间', trigger: 'blur'},
                    ]
                }
            };
        },
        computed: {
            ...mapState({
                readerInfo: (state) => state.Borrow.readerInfo
            }),
            ...mapState({
                borrowInfoList: (state) => state.Borrow.borrowInfoList
            }),
            ...mapState({
                queryResult: (state) => state.Book.bookList
            }),
            ...mapState({
                loginReader: (state) => state.Reader.loginReader
            }),
        },
        activated() {
            this.$store.dispatch("clear")
        }
        ,
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.searchParams = this.ruleForm
                        this.$store.dispatch("queryBook", this.searchParams)
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleClickUpdate() {
                this.$router.replace("/book/updateBook/edit")
            },
            handleClickDelete() {
                this.searchParams = {}
                this.searchParams.id = this.queryResult[0].bkId

                this.deleteBookFn(this.searchParams)
            },
            async deleteBookFn(params = {}) {
                let response = await deleteBook(params);
                if (response.code == 200) {
                    alert("删除成功")
                    this.$store.dispatch("queryBook", params)

                }
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            handleClickBorrow(row) {
                if(this.queryResult[0].bkStatus=="借出"){
                    this.drawer=false
                    alert(this.queryResult[0].bkName+"已借出")
                }else if (this.queryResult[0].bkStatus=="在馆"){
                    this.drawer = true
                    this.borrowForm.rdId = this.readerInfo[0].rdId
                    this.borrowForm.rdName = this.readerInfo[0].rdName
                    this.borrowForm.bkId = this.queryResult[0].bkId
                    this.borrowForm.bkName = this.queryResult[0].bkName
                    this.borrowForm.operatorBorrow=this.loginReader.username
                }

            },
            commitBorrow() {
                if (!this.borrowForm.ldDateRetPlan==''){
                    this.borrowBookFn(this.borrowForm)
                }else {
                    alert("选择归还时间")
                }

            },
            async borrowBookFn(params = {}) {
                let response = await borrowBook(params);
                if (response.code == 200) {
                    alert("借书成功")
                   this.$router.replace("/borrow/borrowBook")
                    this.drawer=false
                }
            },
        }


    }
</script>

<style scoped>
    .formContainer {
        width: 500px;
        margin: 0 auto;
    }

    .tableContainer {
        position: relative;
        right: 0px;
        left: 0px;
    }
</style>
